<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div style="margin-top: 10px">
        <el-button class="filter-item" size="mini" type="success" icon="el-icon-zoom-in" @click="add">发布教学评价
        </el-button>
      </div>
    </div>
    <e-form :is-add="isAdd" @close="load" ref="form"></e-form>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="loading"
      :data="data"
      size="small"
      style="width: 100%;"
      stripe
      empty-text="暂无数据"
      highlight-current-row
      current-row-key="id"
      @select="handleSelectionChange"
    >
      <el-table-column prop="tname" label="学年学期" min-width="100"/>
      <el-table-column prop="commentBatch" label="评价批次" min-width="200"/>
      <el-table-column prop="beginTime" label="开始时间" show-overflow-tooltip min-width="100">
      </el-table-column>
      <el-table-column prop="endTime" label="结束时间" show-overflow-tooltip min-width="100">
      </el-table-column>
      <el-table-column label="操作"
                       align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="info" icon="el-icon-edit" @click="edit(scope.row)">修改
          </el-button>
          <el-button size="mini" type="danger" icon="el-icon-edit" @click="subDelete(scope.row.id)">撤回
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-row>
      <el-col :offset="10">
        <el-pagination
          :total="total"
          :current-page="params.offset"
          style="margin-top: 8px;"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, prev, pager, next, sizes"
          @size-change="sizeChange"
          @current-change="pageChange"/>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import service from '../../../utils/request'
  import eForm from './form'
  import comment from '@/api/admin/comment/comment'
  import { pageQuery } from '@/api/admin/comment/comment'

  export default {
    components:{
      eForm
    },
    data() {
      return {
        loading: true,
        total: 0,
        show: false,
        isAdd: false,
        classesId: null,
        classname: null,
        params: {
          offset: 1,
          limit: 10,
          keyword: null,
          status: null
        },
        data: null
      }
    },
    created() {
      this.load()
    },
    methods: {
      load() {
        pageQuery(this.params).then(res => {
          this.data = res.records
          this.loading = false
          this.currentPage = res.current
          this.total = res.realTotal
        }).catch(error => {
          this.loading = false
        })
      },
      sizeChange(val) {
        this.params.limit = val
        this.load()
      },
      pageChange(val) {
        this.params.offset = val
        this.load()
      },
      toQuery() {
        this.load()
      },
      selectList(val) {
        this.$router.push({path:"teacherTeamComment",query:{"commentId":val.id}})
      },
      handleSelectionChange(val, row) {
        if (val.length == 0) {
          this.classesId = null
          this.classname = null
        } else if (val.length > 1) {
          this.$refs.table.clearSelection()
          this.$refs.table.toggleRowSelection(row)
          val.splice(0, val.length - 1)
          this.classesId = val[0].id
          this.classname = val[0].classname
        } else {
          this.classesId = val[0].id
          this.classname = val[0].classname
        }
      },
      subDelete(id) {
        this.$confirm('确定进行此操作吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          comment.del(id).then(res => {
            this.$notify({
              title: '成功',
              type: 'success',
              duration: 2500
            })
            this.load()
          }).catch(err => {
            console.log(err.response.data.message)
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
      },
      add() {
        this.isAdd = true
        this.$refs.form.dialog = true
      },
      edit(data) {
        this.isAdd = false
        const _this = this.$refs.form
        _this.form = data
        _this.dialog = true
      },
    }
  }
</script>

<style scoped>
  .status {
    color: green;
  }
</style>
